package com.wzdigit.wms.wms.controller.task;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.common.utils.ValidatorUtil;
import com.wzdigit.wms.basic.client.Pager;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.task.UpdateTaskSchedulingRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.task.TaskSchedulingReponse;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.wms.service.task.TaskScheduleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * wms定时调度任务表 前端控制器
 * </p>
 *
 * @author 
 * @since 2021-04-26
 */
@RestController
@Api(tags = "WMS定时调度任务管理")
@RequestMapping(value = WmsWareConstant.API_VERSION)
public class TaskSchedulingController {

	public static final String REQUEST_MODEL = "/taskscheduling";

    @Autowired
    private TaskScheduleService taskScheduleService;
    
    /**
	 * 查询所有的调度任务
	 * @param dto
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "查询所有的调度任务",notes = "查询所有的调度任务")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/searchTaskSchedule")
    public PagerInfo<List<TaskSchedulingReponse>> searchTaskSchedule(
    		Pager<?> pager,
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return taskScheduleService.searchTaskSchedule(pager);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
	
	/**
     * 修改调度任务
     * @param dto
     * @param bindingResult
     * @param servletRequest
     * @param servletResponse
     * @return
	 * @throws NoSuchFieldException 
     */
    @ApiOperation(value = "修改调度任务",notes = "修改调度任务")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PutMapping(value = REQUEST_MODEL + "/updateTaskSchedule")
    public Result<?> updateTaskSchedule(
    		@RequestBody @Valid UpdateTaskSchedulingRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) throws Exception {
        try {
        	ValidatorUtil.validator(bindingResult);
            return taskScheduleService.updateTaskSchedule(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
}
